package com.yonyou.train.service;

import com.yonyou.train.ResultJson;
import com.yonyou.train.StatusCode;
import com.yonyou.train.dao.CourseNoticeDao;
import com.yonyou.train.exception.PreviewServiceException;
import com.yonyou.train.model.Class;
import com.yonyou.train.model.Class_Student;
import com.yonyou.train.model.Class_Sub;
import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class CourseNoticeService {
    @Autowired
    private CourseNoticeDao courseNoticeDao;


    public List queryCourse(String token) {
        List list = new ArrayList();
        List<Class> course = courseNoticeDao.queryCourse(token);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd（E）HH:mm", Locale.CHINA);
        SimpleDateFormat s = new SimpleDateFormat("HH:mm");

        for (Class cour : course) {
            Map map = new HashMap();
            map.put("class_id", cour.getId());
            map.put("teacher", cour.getTeacher_name());
            map.put("assistant", cour.getAssistant_name());
            map.put("subject", cour.getSubject());
            map.put("teacher_id", cour.getTeacher_id());
            map.put("assistant_id", cour.getAssistant_id());

            List<Class_Sub> subCourse = courseNoticeDao.querySubCourse(cour.getId(),token);
            if(subCourse.size()!=0) {
                List<Map> subinfo = new ArrayList<Map>();
                for (Class_Sub sub : subCourse) {
                    Map m = new HashMap();
                    m.put("time", sdf.format(sub.getStarttime()).replaceAll("星期", "周") + "-" + s.format(sub.getEndtime()));
                    m.put("address", sub.getAddress());
                    m.put("subclass_id",sub.getId());


                    subinfo.add(m);

                }
                Map M = new HashMap();

                M.put("class", map);
                M.put("subclass", subinfo);


                list.add(M);
            }


        }

        return list;
    }
    public Object updateSigndate (Map map, String token ){

        int a =(int)map.get("class_id");
        long aa=(long)a;
        int b=(int)map.get("subclass_id");
        long bb=(long)b;
        double latitude = Double.parseDouble(map.get("latitude").toString());
        double longitude = Double.parseDouble(map.get("longitude").toString());
        String address = (String)map.get("address");
        Date date=new Date();
//        Class_Student student=courseNoticeDao.findsubClassbyId(aa,bb,token);
        Class_Sub sub=courseNoticeDao.findDateById(aa,bb);
        if(date.getTime()>=sub.getStarttime().getTime() && date.getTime()<=sub.getEndtime().getTime()){
            return courseNoticeDao.updateSignstate(longitude,latitude,address,aa,bb,token);
        }else{
           throw new PreviewServiceException("当前时间不能签到");
        }

    }
}
